/**
 * Created by IntelliJ IDEA.
 * User: martlenn
 * Date: 25-Jan-2008
 * Time: 01:55:26
 */
package org.hkupp.util.workers;

import be.proteomics.util.sun.SwingWorker;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.Collection;
import java.util.ArrayList;

import org.hkupp.gui.progressbar.DefaultProgressBar;
/*
 * CVS information:
 *
 * $Revision$
 * $Date$
 */

/**
 * This class is a SwingWorker which finds all tag summary files recursively
 * from a top-level folder.
 *
 *
 * @author Lennart Martens
 * @version $Id$
 */
public class TagSummaryFileFinderWorker extends SwingWorker {

    private File iParent = null;
    private DefaultProgressBar iProgress = null;

    public TagSummaryFileFinderWorker(File aParent, DefaultProgressBar aProgress) {
        this.iParent = aParent;
        this.iProgress = aProgress;
    }

    public Object construct() {
        if(iProgress != null) {
            iProgress.setMessage("Locating tag summary files...");
        }
        Collection<File> files = new ArrayList<File>();
        FilenameFilter ff = new InnerFilenameFilter();

        recurseForTagSummaryFiles(iParent, files, ff);

        File[] result = new File[files.size()];
        files.toArray(result);
        if(iProgress != null) {
            iProgress.dispose();
        }
        return result;
    }

    private void recurseForTagSummaryFiles(File aParent, Collection<File> aFiles, FilenameFilter aFilenameFilter) {
        File[] list = aParent.listFiles(aFilenameFilter);
        for (int i = 0; i < list.length; i++) {
            File file = list[i];
            if(file.isDirectory()) {
                recurseForTagSummaryFiles(file, aFiles, aFilenameFilter);
            } else if(file.getName().equals("tagSummary.1.tsv")) {
                aFiles.add(file);
            } else {
                System.err.println(" +++ This should never happen! A file made it past the filenamefilter whose name is not 'tagSummary.1.tsv'!");
            }
        }
    }

    private class InnerFilenameFilter implements java.io.FilenameFilter {
        /**
         * Tests if a specified file should be included in a file list.
         *
         * @param dir  the directory in which the file was found.
         * @param name the name of the file.
         * @return <code>true</code> if and only if the name should be
         *         included in the file list; <code>false</code> otherwise.
         */
        public boolean accept(File dir, String name) {
            boolean result = false;

            File file = new File(dir, name);
            // Tag summaryfiles can pass...
            if(!file.isDirectory() && name.equals("tagSummary.1.tsv")) {
                result = true;
            }

            // As can folders that are NOT 'cpick_in' or 'results_mstag'.
            if(file.isDirectory() && !(name.equals("cpick_in") || name.equals("results_mstag"))) {
                result = true;
            }

            return result;
        }
    }
}
